<?php
/**
 * 创建人 : Forpastime
 * 创建时间: 2023/3/30 15:33
 * 个人主页 : http://forpastime.com
 */

namespace App\Controller\Admin;

use App\Model\Article;
use App\Model\ArticleCategory;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Contract\RequestInterface;
use App\Utils\Result;
use App\Middleware\Auth\AdminTokenMiddleware;
use App\Middleware\Auth\AdminPowerMiddleware;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use App\Annotation\AdminNode;

/**
 * @AdminNode(name="文章管理",isMenu="1",sort=100,style="layui-icon-list")
 * @Middlewares({
 *      @Middleware(AdminTokenMiddleware::class),
 *      @Middleware(AdminPowerMiddleware::class)
 * })
 *
 * @AutoController(prefix="admin/article")
 * Class ArticleController
 * @package App\Controller\Admin
 */
class ArticleController
{
    /**
     * @Inject()
     * @var RequestInterface
     */
    protected $request;

    /**
     * @Inject()
     * @var Article
     */
    protected $article;

    /**
     * @Inject()
     * @var ArticleCategory
     */
    protected $category;


    /**
     * @AdminNode(name="文章列表",isMenu="1")
     * @return mixed
     */
    public function index()
    {
        $param = $this->request->all();
        $where = [];
        if(isset($param['status']) && $param['status']!=''){
            $where[] = ['status',$param['status']];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['title','like','%'.$param['title'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $start = strtotime(explode('~',$param['created_at'])[0]);
            $end =  strtotime(explode('~',$param['created_at'])[1]);
            $where[] = ['created_at','>=',$start];
            $where[] = ['created_at','<=',$end];
        }
        $res = $this->article->getList($where);
        return Result::success($res);
    }

    /**
     * @AdminNode(name="文章分类",isMenu="1")
     * @return mixed
     */
    public function category()
    {
        $param = $this->request->all();
        $where = [];
        if(isset($param['status']) && $param['status']!=''){
            $where[] = ['status',$param['status']];
        }
        $res = $this->category->getList($where);
        return Result::success($res);
    }

    /**
     * @AdminNode(name="文章详情",isMenu="0")
     * @return mixed
     */
    public function getInfo()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id'])){
            return Result::error('参数异常');
        }
        $res = $this->article->getOne($param['id']);
        if($res && !empty($res)){
            $res['title'] = htmlspecialchars_decode(html_entity_decode($res['title']));
            $res['description'] = htmlspecialchars_decode(html_entity_decode($res['description']));
            $res['content'] = htmlspecialchars_decode(html_entity_decode($res['content']));
            return Result::success($res);
        }else{
            return Result::error('未找到相关信息');
        }
    }

    /**
     * @AdminNode(name="添加文章",isMenu="0")
     * @return mixed
     */
    public function addArticle()
    {
        $param = $this->request->all();
        $res = $this->article->add($param);
        return Result::success($res);
    }

    /**
     * @AdminNode(name="编辑文章",isMenu="0")
     * @return mixed
     */
    public function editArticle()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id'])){
            return Result::error('参数异常');
        }
        $res = $this->article->edit($param['id'],$param);
        return Result::success($res);
    }

    /**
     * @AdminNode(name="禁用文章",isMenu="0")
     * @return mixed
     */
    public function delArticle()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id'])){
            return Result::error('参数异常');
        }
        $res = $this->article->edit($param['id'],['status'=>3]);
        return Result::success($res);
    }


    /**
     * @AdminNode(name="添加分类",isMenu="0")
     * @return mixed
     */
    public function addCategory()
    {
        $param = $this->request->all();
        $res = $this->category->add($param);
        return Result::success($res);
    }

    /**
     * @AdminNode(name="修改分类",isMenu="0")
     * @return mixed
     */
    public function editCategory()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id'])){
            return Result::error('参数异常');
        }

        if($this->request->isMethod('post')){
            $param['category_name'] = htmlspecialchars(htmlentities($param['category_name']));
            $res = $this->category->edit($param['id'],$param);
            if($res){
                return Result::success([]);
            }else{
                return Result::error('未找到相关信息');
            }
        }else{
            $res = $this->category->getOne($param['id']);
            if($res && !empty($res)){
                $res['category_name'] = htmlspecialchars_decode(html_entity_decode($res['category_name']));
                return Result::success($res);
            }else{
                return Result::error('未找到相关信息');
            }
        }
    }

    /**
     * @AdminNode(name="删除分类",isMenu="0")
     * @return mixed
     */
    public function delCategory()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id'])){
            return Result::error('参数异常');
        }
        $res = $this->category->edit($param['id'],['status'=>0]);
        return Result::success($res);
    }
}